package com.health.userbackstage.web.control;

import com.github.pagehelper.PageInfo;
import com.health.userbackstage.pojo.CheckGroup;
import com.health.userbackstage.pojo.CheckItem;
import com.health.userbackstage.pojo.Member;
import com.health.userbackstage.pojo.result.Result;
import com.health.userbackstage.pojo.result.ResultEnum;
import com.health.userbackstage.service.CheckGroupService;
import com.health.userbackstage.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    @Autowired
    private CheckItemService checkItemService;

    @GetMapping("/select")
    public Result findAll(Integer currentPage,Integer pageSize,String queryString){
        PageInfo<CheckGroup> checkgroups = checkGroupService.findPage(currentPage,pageSize);

        return Result.success(checkgroups);
    }

    @GetMapping("/findbyid")
    public Result findbyid(Integer id){
        return Result.success(id);
    }

    @GetMapping("/findcheckitems")
    public Result findCheckItems(){
        List<CheckItem> checkitems = checkItemService.list();

        return Result.success(checkitems);
    }

    @GetMapping("/findciid")
    public Result findciid(Integer cgid){
        List<Integer> ciids = checkGroupService.findciid(cgid);

        return Result.success(ciids);
    }


    @GetMapping("{queryString}")
    public Result findOne(@PathVariable("queryString") String queryString) {
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setCode(queryString);
        CheckGroup one = checkGroupService.selectOneByCode(checkGroup);
        List<CheckGroup> list = new ArrayList<>();
        if (one == null){
            checkGroup.setCode(null);
            checkGroup.setName(queryString);
            CheckGroup two = checkGroupService.selectOneByName(checkGroup);
            if (two != null){
                list.add(two);
            }else{
                checkGroup.setName(null);
                checkGroup.setHelpCode(queryString);
                CheckGroup three = checkGroupService.selectOneByHelpCode(checkGroup);
                if(three != null){
                    list.add(three);
                }else{
                    return Result.fail(ResultEnum.FAILED_FIND);
                }
            }
        }else {
            list.add(one);
        }
        return Result.success(list);
    }


    @PutMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup) {
        int update = checkGroupService.update(checkGroup);
        if (update!=0){
            return Result.success("修改成功");
        }else{
            return Result.fail(ResultEnum.FAILED_EDIT);
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup) {

        Result error = CheckGroupController.error(checkGroup);
        if (error.getCode()==400){
            return error;
        }

        int add = checkGroupService.add(checkGroup);

        if (add != 0) {
            return Result.success("添加成功");
        } else {
            return Result.fail("添加失败");
        }
    }


    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Integer id){
        boolean flag = checkGroupService.del(id);


        if(flag){
            return Result.success("删除成功");
        }else{
            return Result.fail("删除失败");
        }
    }

    public static Result error(CheckGroup checkGroup){

        //判断编码
        if (checkGroup.getCode() == null ){
            return Result.fail("请输入编码");
        }

        //判断名称
        if (checkGroup.getName() == null ){
            return Result.fail("请输入名称");
        }

        //判断性别
        if (checkGroup.getSex() == null ){
            return Result.fail("请输入性别");
        }

        //判断助记码
        if (checkGroup.getHelpCode() == null ){
            return Result.fail("请输入助记码");
        }

        //判断说明
        if (checkGroup.getRemark() == null ){
            return Result.fail("请输入说明");
        }

        //判断操作
        if (checkGroup.getAttention() == null ){
            return Result.fail("请输入操作");
        }

        return Result.success();
    }

}
